using System;
using System.Collections.Generic;
using FireStarterModeller.BO;
using FireStarterModeller.Logic;
using Habanero.Base;
using NUnit.Framework;
 

namespace FireStarterModeller.Test
{
    // Provides a part of the TestUtilsBase class that creates sample DBTable objects.
    // If these methods create invalid data, then implement custom overrides in the
    // TestUtils class.

    public static class TestUtilsShared
    {
        #region Bo Creators

        public static DMSolution CreateSavedDMSolution()
        {
            DMSolution dmSolution = CreateUnsavedValidDMSolution();
            dmSolution.Save();
            return dmSolution;
        }

        public static DMSolution CreateUnsavedValidDMSolution()
        {
            SolutionCreator creator = new SolutionCreator();
            DMSolution dmSolution = creator.CreateSolution();
            dmSolution.SolutionName = "A" + GetRandomString(2, 8);
            dmSolution.SolutionDirectory = GetRandomString();
            dmSolution.Generated = false;
            //dmSolution.DefaultAssembly = CreateUnsavedValidProjectAssemblyInfo(dmSolution);

            return dmSolution;
        }

        public static DMClass CreateSavedDMClass()
        {
            DMClass dmClass = CreateUnsavedValidDMClass();
            //DMProperty dmProperty = CreateUnsavedValidDMProperty(dmClass);
            //dmClass.CreateObjectIdentity(dmProperty);
            dmClass.Save();
            return dmClass;
        }

/*
        public static BusinessObjectCollection<DMClass> CreateSavedDMClasssCollection()
        {
            CreateSavedDMClass();
            CreateSavedDMClass();
            BusinessObjectCollection<DMClass> businessObjectInfosCollection = new BusinessObjectCollection<DMClass>();
            businessObjectInfosCollection.LoadAll();
            return businessObjectInfosCollection;
        }
*/

        public static DMClass CreateUnsavedValidDMClass()
        {
            DMSolution dmSolution = CreateSavedDMSolution();
            DMClass dmClass = dmSolution.DefaultAssembly.Classes.CreateBusinessObject();
            return UpdateBusinessObjectWithValidData(dmClass);
        }

        public static DMClass CreateUnsavedValidDMClass(IDMSolution solution)
        {
            DMClass dmClass = solution.DefaultAssembly.Classes.CreateBusinessObject();
            return UpdateBusinessObjectWithValidData(dmClass);
        }

        public static DMClass UpdateBusinessObjectWithValidData(DMClass dmClass)
        {
            dmClass.ClassNameBO = GetRandomString(1, 5);
            dmClass.MappedTableName = GetRandomString();
            dmClass.DisplayName = GetRandomString();
            return dmClass;
        }

        public static DMClass CreateSavedDMClassWithProps()
        {
            DMClass dmClass = CreateSavedDMClass();
            CreateSavedDMProperty(dmClass);
            CreateSavedDMProperty(dmClass);
            return dmClass;
        }

        public static DMClass CreateSavedDMClassWithUniqueConstraints()
        {
            DMClass dmClass = CreateSavedDMClass();
            CreateSavedUniqueConstraintInfo(dmClass);
            CreateSavedUniqueConstraintInfo(dmClass);
            return dmClass;
        }

        public static DMClass CreateSavedDMClassWithRelationships()
        {
            DMClass dmClass = CreateSavedDMClass();
            CreateSavedRelationshipInfo(dmClass);
            CreateSavedRelationshipInfo(dmClass);
            return dmClass;
        }

        public static DMProperty UpdateDMPropertyWithValidData(DMProperty dmProperty)
        {
            dmProperty.PropertyName = GetRandomString();
            dmProperty.Generated = false;
            if (dmProperty.OrdinalPosition <= 0)
            {
                dmProperty.OrdinalPosition = GetRandomInt(0, 1000000);
            }
            if (dmProperty.PropertyType == null)
            {
                if (dmProperty.Class != null && dmProperty.Class.Solution != null)
                {
                    dmProperty.PropertyType = dmProperty.Class.Solution.GetDMPropertyType("System.String");
                }
                if (dmProperty.PropertyType == null) dmProperty.PropertyType = CreateSavedPropertyTypeInfo();
            }
            if (dmProperty.ReadWriteRule == null)
            {
                dmProperty.ReadWriteRule = PropReadWriteRule.ReadWrite;
            }

            return dmProperty;
        }

        public static DMClass CreateSavedDMClass(DMAssembly dmAssembly)
        {
            DMClass dmClass = dmAssembly.Classes.CreateBusinessObject();
            UpdateBusinessObjectWithValidData(dmClass);
            DMProperty dmProperty = CreateUnsavedValidDMProperty(dmClass);
            dmClass.CreateObjectIdentity(dmProperty);
            dmClass.Save();
            return dmClass;
        }

        public static DMProperty CreateSavedDMProperty(IDMClass dmClass)
        {
            DMProperty property = CreateUnsavedDMProperty(dmClass);
            property.Save();
            return property;
        }


        public static DMProperty CreateUnsavedDMProperty(IDMClass dmClass)
        {
            DMProperty property = TestUtilsDMProperty.CreateUnsavedValidDMProperty(dmClass);
            UpdateDMPropertyWithValidData(property);
            var dmClass1 = property.Class;
            if (dmClass1 != null)
            {
                IDMSolution solution = dmClass1.Solution;
                property.PropertyType = solution.GetDMPropertyType("System.String");
            }
            return property;
        }


        public static DMProperty CreateSavedDMProperty()
        {
            DMProperty dmProperty = CreateUnsavedValidDMProperty();
            dmProperty.Save();
            return dmProperty;
        }

        public static DMProperty CreateUnsavedValidDMProperty()
        {
            return CreateUnsavedValidDMProperty(null);
        }

        public static DMProperty CreateUnsavedValidDMProperty(IDMClass dmClass)
        {
            if (dmClass == null) dmClass = CreateSavedDMClass();
            DMProperty dmProperty = dmClass.Properties.CreateBusinessObject();
            dmProperty.PropertyName = GetRandomString();
            //dmProperty.PropertyType = CreateSavedPropertyTypeInfo();
            dmProperty.Generated = false;
            dmProperty.OrdinalPosition = GetRandomInt(0, 3);
            if (dmProperty.Class != null && dmProperty.Class.Solution != null)
            {
                dmProperty.PropertyType = dmProperty.Class.Solution.GetDMPropertyType("System.String");
            }
            return dmProperty;
        }

        public static DMObjectIdentity CreateSavedObjectIdentityInfo(DMClass dmClass)
        {
            DMObjectIdentity primaryKey = CreateUnsavedValidObjectIdentityInfo(dmClass);
            primaryKey.Save();
            return primaryKey;
        }

        public static DMObjectIdentity CreateSavedObjectIdentityInfo()
        {
            DMObjectIdentity primaryKey = CreateUnsavedValidObjectIdentityInfo();
            primaryKey.Save();
            return primaryKey;
        }

        public static DMObjectIdentity CreateUnsavedValidObjectIdentityInfo()
        {
            DMClass dmClass = CreateUnsavedValidDMClass();
            DMObjectIdentity primaryKey = new DMObjectIdentity();
            dmClass.ObjectIdentity = primaryKey;
            return UpdateObjectIdentityInfoWithValidData(primaryKey);
        }
        public static DMObjectIdentity CreateUnsavedValidObjectIdentityInfoWithTwoProps()
        {
            DMClass dmClass = CreateUnsavedValidDMClass();
            DMObjectIdentity primaryKey = new DMObjectIdentity();
            dmClass.ObjectIdentity = primaryKey;
            primaryKey.IsObjectID = false;

            AddDMProperty(primaryKey);
            AddDMProperty(primaryKey);

            return primaryKey;
        }

        private static void AddDMProperty(DMObjectIdentity primaryKey)
        {
            DMProperty dmProperty = CreateUnsavedValidDMProperty(primaryKey.Class);
            dmProperty.PropertyTypeString = "System.Guid";
            dmProperty.ObjectIdentity = primaryKey;
        }

        public static DMObjectIdentity CreateUnsavedValidObjectIdentityInfo(DMClass dmClass)
        {
            DMObjectIdentity primaryKey = new DMObjectIdentity();

            dmClass.ObjectIdentity = primaryKey;
          
            return UpdateObjectIdentityInfoWithValidData(primaryKey);
        }

        private static DMObjectIdentity UpdateObjectIdentityInfoWithValidData(DMObjectIdentity primaryKey)
        {
            primaryKey.IsObjectID = false;
            if (primaryKey.Properties.Count == 0)
            {
                DMProperty dmProperty = CreateUnsavedValidDMProperty(primaryKey.Class);
                dmProperty.PropertyTypeString = "System.Guid";
                dmProperty.ObjectIdentity = primaryKey;
            }
            return primaryKey;
        }

        public static DMUniqueConstraint CreateSavedUniqueConstraintInfoWithProperties()
        {
            DMClass dmClass = CreateSavedDMClassWithProps();
            CreateSavedDMProperty(dmClass);
            DMUniqueConstraint dmUniqueConstraint = CreateSavedUniqueConstraintInfo(dmClass);
            CreateSavedUniqueConstraintPropertyInfo(dmUniqueConstraint, dmClass.Properties[0]);
            CreateSavedUniqueConstraintPropertyInfo(dmUniqueConstraint, dmClass.Properties[2]);
            return dmUniqueConstraint;
        }

        public static DMUniqueConstraint CreateSavedUniqueConstraintInfo()
        {
            DMClass dmClass = CreateSavedDMClass();
            return CreateSavedUniqueConstraintInfo(dmClass);
        }

        private static DMUniqueConstraint CreateSavedUniqueConstraintInfo(DMClass dmClass)
        {
            DMUniqueConstraint dmUniqueConstraint = CreateUnsavedValidUniqueConstraintInfo(dmClass);
            dmUniqueConstraint.Save();
            return dmUniqueConstraint;
        }

        public static DMUniqueConstraint CreateUnsavedValidUniqueConstraintInfo()
        {
            DMClass dmClass = CreateSavedDMClass();
            return CreateUnsavedValidUniqueConstraintInfo(dmClass);
        }

        private static DMUniqueConstraint CreateUnsavedValidUniqueConstraintInfo(DMClass dmClass)
        {
            DMUniqueConstraint dmUniqueConstraint = dmClass.UniqueConstraints.CreateBusinessObject();
            return UpdateUniqueConstraintInfoWithValidData(dmUniqueConstraint);
        }

        private static void CreateUnsavedValidUniqueConstraintInfo(DMRelationship dmRelationship)
        {
            CreateUnsavedUniqueConstraintInfoForOneSideOfRelationship(dmRelationship);
            CreateUnsavedUniqueConstraintInfoForOneSideOfRelationship(dmRelationship.ReverseRelationship);
        }

        private static void CreateSavedValidUniqueConstraintInfo(DMRelationship dmRelationship)
        {
            DMUniqueConstraint dmUniqueConstraint = CreateUnsavedUniqueConstraintInfoForOneSideOfRelationship(dmRelationship);
            dmUniqueConstraint.Save();

            DMUniqueConstraint dmRelatedClassUniqueConstraint = CreateUnsavedUniqueConstraintInfoForOneSideOfRelationship(dmRelationship.ReverseRelationship);
            dmRelatedClassUniqueConstraint.Save();
        }

        private static DMUniqueConstraint CreateUnsavedUniqueConstraintInfoForOneSideOfRelationship(IDMRelationship relationship) {
            DMUniqueConstraint dmUniqueConstraint = relationship.OwnerClass.UniqueConstraints.CreateBusinessObject();
            UpdateUniqueConstraintInfoWithValidData(dmUniqueConstraint);
            foreach (var relationshipProperty in relationship.RelationshipProperties)
            {
                DMUniqueConstraintProperty uniqueConstraintProperty =
                    dmUniqueConstraint.UniqueConstraintProperties.CreateBusinessObject();
                uniqueConstraintProperty.Property = (DMProperty) relationshipProperty.OwnerProperty;
            }
            return dmUniqueConstraint;
        }


        private static DMUniqueConstraint UpdateUniqueConstraintInfoWithValidData(DMUniqueConstraint dmUniqueConstraint)
        {
            dmUniqueConstraint.IgnoreIfNull = true;
            dmUniqueConstraint.Generated = true;
            dmUniqueConstraint.Message = GetRandomString();
            dmUniqueConstraint.UniqueConstraintName = GetRandomString();
            return dmUniqueConstraint;
        }

        public static DMUniqueConstraintProperty CreateSavedUniqueConstraintPropertyInfo()
        {
            DMUniqueConstraint dmUniqueConstraint = CreateSavedUniqueConstraintInfo();
            return CreateSavedUniqueConstraintPropertyInfo(dmUniqueConstraint);
        }

        public static DMUniqueConstraintProperty CreateSavedUniqueConstraintPropertyInfo
            (DMUniqueConstraint dmUniqueConstraint)
        {
            DMUniqueConstraintProperty dmUniqueConstraintProperty = CreateUnsavedValidUniqueConstraintPropertyInfo
                (dmUniqueConstraint);
            dmUniqueConstraintProperty.Save();
            return dmUniqueConstraintProperty;
        }

// ReSharper disable UnusedMethodReturnValue.Local
        private static DMUniqueConstraintProperty CreateSavedUniqueConstraintPropertyInfo
// ReSharper restore UnusedMethodReturnValue.Local
            (DMUniqueConstraint dmUniqueConstraint, DMProperty dmProperty)
        {
            DMUniqueConstraintProperty dmUniqueConstraintProperty =
                dmUniqueConstraint.UniqueConstraintProperties.CreateBusinessObject();
            dmUniqueConstraintProperty.Property = dmProperty;
            dmUniqueConstraintProperty.Save();
            return dmUniqueConstraintProperty;
        }

        public static DMUniqueConstraintProperty CreateUnsavedValidUniqueConstraintPropertyInfo()
        {
            DMUniqueConstraint dmUniqueConstraint = CreateSavedUniqueConstraintInfo();
            return CreateUnsavedValidUniqueConstraintPropertyInfo(dmUniqueConstraint);
        }

        private static DMUniqueConstraintProperty CreateUnsavedValidUniqueConstraintPropertyInfo
            (DMUniqueConstraint dmUniqueConstraint)
        {
            DMUniqueConstraintProperty dmUniqueConstraintProperty =
                dmUniqueConstraint.UniqueConstraintProperties.CreateBusinessObject();
            return UpdateUniqueConstraintPropertyInfoWithValidData(dmUniqueConstraintProperty);
        }

        private static DMUniqueConstraintProperty UpdateUniqueConstraintPropertyInfoWithValidData
            (DMUniqueConstraintProperty dmUniqueConstraintProperty)
        {
            DMClass dmClass = dmUniqueConstraintProperty.UniqueConstraint.Class;
            DMProperty property = CreateSavedDMProperty(dmClass);
            dmUniqueConstraintProperty.Property = property;
            return dmUniqueConstraintProperty;
        }
/*
        public static DMPropRuleParameter CreateSavedPropRuleInfoParameter()
        {
            DMPropRuleParameter parameter = new DMPropRuleParameter
                                                {
                                                    KeyName = GetRandomString(),
                                                    Value = GetRandomString(),
                                                    PropRule = CreateSavedPropRuleInfo()
                                                };
            parameter.Save();
            return parameter;
        }*/

        public static DMPropRule CreateSavedPropRuleInfo()
        {
            DMPropRule dmPropRule = new DMPropRule
                                        {
                                            PropRuleName = GetRandomString(),
                                            Generated = true,
                                            DMProperty = CreateSavedDMProperty(CreateSavedDMClass())
                                        };
            dmPropRule.Save();
            return dmPropRule;
        }
/*
        public static DMPropRuleParameter CreateUnsavedValidRule()
        {
            DMPropRuleParameter parameter = new DMPropRuleParameter
                                                {
                                                    KeyName = GetRandomString(),
                                                    Value = GetRandomString(),
                                                    PropRule = CreateSavedPropRuleInfo()
                                                };
            return parameter;
        }*/

        public static DMRelationship CreateSavedRelationshipInfo()
        {
            DMRelationship dmRelationship = CreateUnsavedValidRelationshipInfo();
            dmRelationship.Save();
            return dmRelationship;
        }

// ReSharper disable UnusedMethodReturnValue.Local
        private static DMRelationship CreateSavedRelationshipInfo(DMClass dmClass)
// ReSharper restore UnusedMethodReturnValue.Local
        {
            DMRelationship dmRelationship = CreateUnsavedValidRelationshipInfo(dmClass);
            dmRelationship.Save();
            return dmRelationship;
        }

        public static DMRelationship CreateSavedRelationshipInfoWithTwoRelatedProperties()
        {
            DMRelationship dmRelationship = CreateSavedRelationshipInfo();
            CreateSavedRelatedPropertyInfo(dmRelationship);
            CreateSavedValidUniqueConstraintInfo(dmRelationship);
            return dmRelationship;
        }

        public static DMRelationship CreateUnsavedRelationshipInfoWithTwoRelatedProperties()
        {
            DMRelationship dmRelationship = CreateUnsavedValidRelationshipInfo();
            CreateUnsavedValidRelatedPropertyInfo(dmRelationship);
            CreateUnsavedValidUniqueConstraintInfo(dmRelationship);
            return dmRelationship;
        }

        public static DMRelationship CreateUnsavedValidRelationshipInfo()
        {
            DMClass dmClass = CreateSavedDMClass();
            return CreateUnsavedValidRelationshipInfo(dmClass);
        }

        public static DMRelationship CreateUnsavedValidRelationshipInfo(IDMClass idmClass)
        {
            DMClass dmClass = (DMClass) idmClass;
            DMRelationship dmRelationship = dmClass.OwnerRelationships.CreateBusinessObject();
            UpdateRelationshipInfoWithValidData(dmRelationship.ReverseRelationship);
            UpdateRelationshipInfoWithValidData(dmRelationship);
            CreateUnsavedValidRelatedPropertyInfo(dmRelationship);
            return dmRelationship;
         }

        public static IDMRelationship UpdateRelationshipInfoWithValidData(IDMRelationship dmRelationship)
        {
            dmRelationship.RelationshipName = GetRandomString();
            dmRelationship.Cardinality = Cardinality.Single;
            dmRelationship.DeleteAction = DeleteParentAction.DoNothing;
            if (dmRelationship.Status.IsNew && dmRelationship.ClassDM == null)
            {
                DMClass relatedBO = CreateSavedDMClass();//dmRelationship.OwnerClass.Assembly);
                dmRelationship.ClassDM = relatedBO;
            }
            dmRelationship.RelationshipType = RelationshipType.Association;
            //dmRelationship.RelatedClass = GetRandomString();
            //dmRelationship.RelatedAssembly = GetRandomString();
            return dmRelationship;
        }
/*
        public static DMRelationshipProperty CreateSavedRelatedPropertyInfo()
        {
            DMRelationshipProperty dmRelationshipProperty = CreateUnsavedValidRelatedPropertyInfo();
            dmRelationshipProperty.Save();
            return dmRelationshipProperty;
        }*/

// ReSharper disable UnusedMethodReturnValue.Local
        private static DMRelationshipProperty CreateSavedRelatedPropertyInfo(DMRelationship dmRelationship)
// ReSharper restore UnusedMethodReturnValue.Local
        {
            DMRelationshipProperty dmRelationshipProperty = CreateUnsavedValidRelatedPropertyInfo(dmRelationship);
            dmRelationshipProperty.Save();
            return dmRelationshipProperty;
        }

// ReSharper disable UnusedMethodReturnValue.Local
        private static DMUniqueConstraintProperty CreateSavedRelatedPropertyInfo(DMUniqueConstraint uniqueConstraint)
// ReSharper restore UnusedMethodReturnValue.Local
        {
            DMUniqueConstraintProperty dmRelationshipProperty = CreateUnsavedValidRelatedPropertyInfo(uniqueConstraint);
            dmRelationshipProperty.Save();
            return dmRelationshipProperty;
        }

/*        public static DMRelationshipProperty CreateUnsavedValidRelatedPropertyInfo()
        {
            DMRelationship dmRelationship = CreateSavedRelationshipInfo();
            return CreateUnsavedValidRelatedPropertyInfo(dmRelationship);
        }*/

        private static DMRelationshipProperty CreateUnsavedValidRelatedPropertyInfo(DMRelationship dmRelationship)
        {
            DMRelationshipProperty dmRelationshipProperty = dmRelationship.RelationshipProperties.CreateBusinessObject();
            return UpdateRelatedPropertyInfoWithValidData(dmRelationshipProperty);
        }

        private static DMUniqueConstraintProperty CreateUnsavedValidRelatedPropertyInfo
            (DMUniqueConstraint uniqueConstraint)
        {
            DMUniqueConstraintProperty uniqueConstraintProperty =
                uniqueConstraint.UniqueConstraintProperties.CreateBusinessObject();
            UpdateRelatedPropertyInfoWithValidData(uniqueConstraintProperty);
            return uniqueConstraintProperty;
        }

// ReSharper disable UnusedMethodReturnValue.Local
        private static DMUniqueConstraintProperty UpdateRelatedPropertyInfoWithValidData
// ReSharper restore UnusedMethodReturnValue.Local
            (DMUniqueConstraintProperty uniqueConstraintProperty)
        {
            DMClass owningBO = uniqueConstraintProperty.UniqueConstraint.Class;
            uniqueConstraintProperty.Property = CreateSavedDMProperty(owningBO);
            return uniqueConstraintProperty;
        }

        private static DMRelationshipProperty UpdateRelatedPropertyInfoWithValidData
            (DMRelationshipProperty dmRelationshipProperty)
        {
            var owningBO = dmRelationshipProperty.Relationship.OwnerClass;
            dmRelationshipProperty.RelatedProperty = CreateUnsavedDMProperty(CreateSavedDMClass());
            dmRelationshipProperty.OwnerProperty = CreateUnsavedDMProperty(owningBO);
            return dmRelationshipProperty;
        }

/*        public static DMListItem CreateSavedItemInfo(DMLookupListSimple dmLookupListSimple)
        {
            DMListItem dmListItem = CreateUnsavedValidItemInfo(dmLookupListSimple);
            dmListItem.Save();
            return dmListItem;
        }*/

        public static DMListItem CreateSavedItemInfo()
        {
            DMListItem dmListItem = CreateUnsavedValidItemInfo();
            dmListItem.Save();
            return dmListItem;
        }

        public static DMListItem CreateUnsavedValidItemInfo()
        {
            //DMLookupListSimple dmLookupListSimple = CreateSavedSimpleLookupListInfo();
            DMLookupListSimple dmLookupListSimple = CreateUnsavedValidSimpleLookupListInfo();
            return CreateUnsavedValidItemInfo(dmLookupListSimple);
        }

        public static DMListItem CreateUnsavedValidItemInfo(DMLookupListSimple dmLookupListSimple)
        {
            DMListItem dmListItem = dmLookupListSimple.ListItems.CreateBusinessObject();
            return UpdateItemInfoWithValidData(dmListItem);
        }

        public static DMListItem UpdateItemInfoWithValidData(DMListItem dmListItem)
        {
            dmListItem.Display = GetRandomString();
            dmListItem.Value = GetRandomString();
            return dmListItem;
        }

        public static DMLookupListSimple CreateSavedSimpleLookupListInfo()
        {
            DMLookupListSimple dmLookupListSimple = CreateUnsavedValidSimpleLookupListInfo();
            dmLookupListSimple.Save();
            return dmLookupListSimple;
        }

        public static DMLookupListSimple CreateUnsavedValidSimpleLookupListInfo()
        {
            DMLookupListSimple dmLookupListSimple = new DMLookupListSimple();
            DMListItem item = new DMListItem {Display = GetRandomString(), Value = GetRandomString()};
            dmLookupListSimple.ListItems.Add(item);
            return dmLookupListSimple;
        }

        private static UIView CreateSavedUIInfo()
        {
            DMClass dmClass = CreateSavedDMClass();
            return CreateSavedUIInfo(dmClass);
        }

        private static UIView CreateSavedUIInfo(DMClass dmClass)
        {
            UIView uiView = CreateUnsavedValidUIInfo(dmClass);
            uiView.Save();
            return uiView;
        }

        public static UIView CreateUnsavedValidUIInfo()
        {
            DMClass dmClass = CreateSavedDMClassWithProps();
            return CreateUnsavedValidUIInfo(dmClass);
        }

        private static UIView CreateUnsavedValidUIInfo(DMClass dmClass)
        {
            UIView uiView = dmClass.UIViews.CreateBusinessObject();
            return uiView;
        }

        public static UIGridInfo CreateSavedUIGridInfo()
        {
            UIView uiView = CreateSavedUIInfo();
            return CreateSavedUIGridInfo(uiView);
        }

        private static UIGridInfo CreateSavedUIGridInfo(UIView uiView)
        {
            UIGridInfo uiGrid = CreateUnsavedValidUIGridInfo(uiView);
            uiGrid.Save();
            return uiGrid;
        }

        public static UIGridInfo CreateUnsavedValidUIGridInfo()
        {
            UIView uiView = CreateSavedUIInfo();
            return CreateUnsavedValidUIGridInfo(uiView);
        }

        private static UIGridInfo CreateUnsavedValidUIGridInfo(UIView uiView)
        {
            UIGridInfo uiGrid = new UIGridInfo();
            uiView.UIGridInfo = uiGrid;
            return uiGrid;
        }

/*        public static UIGridColumnInfo CreateSavedUIColumnInfo()
        {
            UIGridInfo uiGrid = CreateSavedUIGridInfo();
            return CreateSavedUIColumnInfo(uiGrid);
        }*/

/*        public static UIGridColumnInfo CreateSavedUIColumnInfo(UIGridInfo uiGrid)
        {
            UIGridColumnInfo uiGridColumn = CreateUnsavedValidUIColumnInfo(uiGrid);
            uiGridColumn.Save();
            return uiGridColumn;
        }*/

        public static UIGridColumnInfo CreateUnsavedValidUIColumnInfo()
        {
            UIGridInfo uiGrid = CreateSavedUIGridInfo();
            return CreateUnsavedValidUIColumnInfo(uiGrid);
        }

        public static UIGridColumnInfo CreateUnsavedValidUIColumnInfo(UIGridInfo uiGrid)
        {
            UIGridColumnInfo uiGridColumn = uiGrid.UIColumns.CreateBusinessObject();
            return UpdateUIColumnInfoWithValidData(uiGridColumn);
        }

        private static UIGridColumnInfo UpdateUIColumnInfoWithValidData(UIGridColumnInfo uiGridColumn)
        {
            uiGridColumn.PropertyName = TestUtilsDMProperty.CreateSavedDMProperty().PropertyName;
            uiGridColumn.Heading = GetRandomString();

            if (uiGridColumn.UIGridColumnControlType == null)
            {
                var dmSolution = uiGridColumn.UIGrid.UIView.Class.Solution;
                uiGridColumn.UIGridColumnControlType = CreateSavedUIGridColumnControlTypeInfo(dmSolution);
            }
            return uiGridColumn;
        }

        public static UIParameter CreateSavedUIParameterInfo()
        {
            UIParameter uiParameter = CreateUnsavedValidUIParameterInfo();
            uiParameter.Save();
            return uiParameter;
        }
/*

        public static UIParameter CreateSavedUIParameterInfo(UIGridColumnInfo gridColumn)
        {
            UIParameter uiParameter = CreateUnsavedValidUIParameterInfo(gridColumn);
            uiParameter.Save();
            return uiParameter;
        }
*/

        public static UIParameter CreateUnsavedValidUIParameterInfo(UIGridColumnInfo gridColumn)
        {
            UIParameter parameter = gridColumn.UIParameters.CreateBusinessObject();
            return UpdateUIParameterInfoWithValidData(parameter);
        }

        public static UIParameter CreateUnsavedValidUIParameterInfo()
        {
            UIParameter parameter = new UIParameter();
            return UpdateUIParameterInfoWithValidData(parameter);
        }

        public static UIParameter UpdateUIParameterInfoWithValidData(UIParameter uiParameter)
        {
            uiParameter.Name = GetRandomString();
            uiParameter.Value = GetRandomString();
            return uiParameter;
        }

        public static UIFormInfo CreateSavedUIFormInfo()
        {
            UIView uiView = CreateSavedUIInfo();
            return CreateSavedUIFormInfo(uiView);
        }

        private static UIFormInfo CreateSavedUIFormInfo(UIView uiView)
        {
            UIFormInfo uiFormInfo = CreateUnsavedValidUIFormInfo(uiView);
            uiFormInfo.Save();
            return uiFormInfo;
        }

        public static UIFormInfo CreateUnsavedValidUIFormInfo()
        {
            UIView uiView = CreateSavedUIInfo();
            return CreateUnsavedValidUIFormInfo(uiView);
        }

        private static UIFormInfo CreateUnsavedValidUIFormInfo(UIView uiView)
        {
            UIFormInfo uiFormInfo = new UIFormInfo();
            uiView.UIFormInfo = uiFormInfo;
            return UpdateUIFormInfoWithValidData(uiFormInfo);
        }

        private static UIFormInfo UpdateUIFormInfoWithValidData(UIFormInfo uiFormInfo)
        {
            uiFormInfo.Width = 300;
            uiFormInfo.Height = 250;
            return uiFormInfo;
        }

        private static UITab CreateSavedUITabInfo()
        {
            UIFormInfo uiFormInfo = CreateSavedUIFormInfo();
            return CreateSavedUITabInfo(uiFormInfo);
        }

        public static UITab CreateSavedUITabInfo(UIFormInfo uiFormInfo)
        {
            UITab uiTab = CreateUnsavedValidUITabInfo(uiFormInfo);
            uiTab.Save();
            return uiTab;
        }

        public static UITab CreateUnsavedValidUITabInfo(UIFormInfo uiFormInfo)
        {
            UITab uiTab = uiFormInfo.UITabs.CreateBusinessObject();
            return UpdateUITabInfoWithValidData(uiTab);
        }

        public static UITab CreateUnsavedValidUITabInfo()
        {
            UIFormInfo uiFormInfo = CreateSavedUIFormInfo();
            return CreateUnsavedValidUITabInfo(uiFormInfo);
        }

        private static UITab UpdateUITabInfoWithValidData(UITab uiTab)
        {
            uiTab.Name = GetRandomString();
            return uiTab;
        }

        private static UIColumnLayout CreateSavedUIColumnLayoutInfo()
        {
            UITab uiTab = CreateSavedUITabInfo();
            return CreateSavedUIColumnLayoutInfo(uiTab);
        }

        public static UIColumnLayout CreateSavedUIColumnLayoutInfo(UITab uiTab)
        {
            UIColumnLayout uiColumnLayout = CreateUnsavedValidUIColumnLayoutInfo(uiTab);
            uiColumnLayout.Save();
            return uiColumnLayout;
        }

        public static UIColumnLayout CreateUnsavedValidUIColumnLayoutInfo()
        {
            UITab uiTab = CreateSavedUITabInfo();
            return CreateUnsavedValidUIColumnLayoutInfo(uiTab);
        }

        public static UIColumnLayout CreateUnsavedValidUIColumnLayoutInfo(UITab uiTab)
        {
            UIColumnLayout uiColumnLayout = uiTab.UIColumnLayouts.CreateBusinessObject();
            return UpdateUIColumnLayoutInfoWithValidData(uiColumnLayout);
        }

        private static UIColumnLayout UpdateUIColumnLayoutInfoWithValidData(UIColumnLayout uiColumnLayout)
        {
            uiColumnLayout.Width = GetRandomInt(0, 100);
            //if (uiColumnLayout.UIForm == null)
            //{
            //    uiColumnLayout.UIForm = uiColumnLayout.UITab.UIFormInfo;
            //}
            return uiColumnLayout;
        }
/*
        public static UIField CreateSavedUIField()
        {
            UIColumnLayout uiColumnLayout = CreateSavedUIColumnLayoutInfo();
            return CreateSavedUIField(uiColumnLayout);
        }*/

        public static UIField CreateSavedUIField(UIColumnLayout uiColumnLayout)
        {
            UIField uiField = CreateUnsavedValidUIField(uiColumnLayout);
            uiField.Save();
            return uiField;
        }

        public static UIField CreateUnsavedValidUIField()
        {
            UIColumnLayout uiColumnLayout = CreateSavedUIColumnLayoutInfo();
            return CreateUnsavedValidUIField(uiColumnLayout);
        }

        public static UIField CreateUnsavedValidUIField(UIColumnLayout uiColumnLayout)
        {
            UIField uiField = uiColumnLayout.UIFields.CreateBusinessObject();
            return UpdateUIFieldWithValidData(uiField);
        }

        private static UIField UpdateUIFieldWithValidData(UIField uiField)
        {
            uiField.LabelText = GetRandomString();
            DMClass dmClass = uiField.UIColumnLayout.UITab.UIFormInfo.UIView.Class;
            DMProperty property = TestUtilsDMProperty.CreateUnsavedValidDMProperty(dmClass);
            property.Save();
            uiField.PropertyName = property.PropertyName;
            if (uiField.UIControlType == null)
            {
                var dmSolution = dmClass.Solution;
                uiField.UIControlType = CreateSavedUIControlTypeInfo((DMSolution) dmSolution);
            }
            return uiField;
        }

        public static DMPropRuleParameter UpdateRulewithValidData(DMPropRuleParameter parameter)
        {
            parameter.KeyName = GetRandomString(100, 10000);
            parameter.Value = GetRandomInt().ToString();

            if (parameter.PropRule == null) parameter.PropRule = CreateSavedPropRuleInfo();
            if (parameter.OrdinalPosition == null) parameter.OrdinalPosition = GetRandomInt();

            return parameter;
        }


        public static DMPropRule CreateSavedPropRuleInfo(DMProperty dmProperty)
        {
            DMPropRule dmPropRule = new DMPropRule
                                        {
                                            PropRuleName = GetRandomString(),
                                            Generated = true,
                                            DMProperty = dmProperty
                                        };
            dmPropRule.Save();
            return dmPropRule;
        }

        public static DMPropRule CreateUnsavedValidPropRuleInfo()
        {
            DMPropRule dmPropRule = new DMPropRule
                                        {
                                            PropRuleName = GetRandomString(),
                                            Generated = true,
                                            DMProperty = CreateSavedDMProperty(CreateSavedDMClass())
                                        };
            return dmPropRule;
        }

        public static DMAssembly CreateDMAssembly()
        {
            DMSolution dmSolution = CreateSavedDMSolution();
            return CreateDMAssembly(dmSolution);
        }

        private static DMAssembly CreateDMAssembly(DMSolution dmSolution)
        {
            DMAssembly dmAssembly = CreateUnsavedValidProjectAssemblyInfo(dmSolution);
            dmAssembly.Save();
            return dmAssembly;
        }

        public static DMAssembly CreateUnsavedValidProjectAssemblyInfo()
        {
            DMSolution dmSolution = CreateSavedDMSolution();
            return CreateUnsavedValidProjectAssemblyInfo(dmSolution);
        }

        public static DMAssembly CreateUnsavedValidProjectAssemblyInfo(DMSolution dmSolution)
        {
            DMAssembly dmAssembly = dmSolution.Assemblies.CreateBusinessObject();
            return UpdateProjectAssemblyInfoWithValidData(dmAssembly);
        }

        private static DMAssembly UpdateProjectAssemblyInfoWithValidData(DMAssembly dmAssembly)
        {
            dmAssembly.Name = GetRandomString();
            return dmAssembly;
        }
/*

        public static BusinessObjectCollection<DMAssembly> CreateSavedProjectAssemblyInfosCollection()
        {
            CreateDMAssembly();
            BusinessObjectCollection<DMAssembly> projectAssemblyInfos = new BusinessObjectCollection<DMAssembly>();
            projectAssemblyInfos.LoadAll();
            return projectAssemblyInfos;
        }
*/

        public static DMInheritanceRelationship CreateSavedDMInheritanceRelationship()
        {
            DMInheritanceRelationship dmInheritanceRelationship = CreateUnsavedValidDMInheritanceRelationship();
            dmInheritanceRelationship.Save();
            return dmInheritanceRelationship;
        }

        public static DMInheritanceRelationship CreateUnsavedValidDMInheritanceRelationship()
        {
            DMInheritanceRelationship dmInheritanceRelationship = new DMInheritanceRelationship();
            return UpdateDMInheritanceRelationshipWithValidData(dmInheritanceRelationship);
        }

        public static DMInheritanceRelationship UpdateDMInheritanceRelationshipWithValidData(DMInheritanceRelationship dmInheritanceRelationship)
        {
            if (dmInheritanceRelationship.SuperClass == null)
            {
                DMClass dmClass = CreateSavedDMClassWithProps();
                dmInheritanceRelationship.SuperClass = dmClass;
            }
            if (dmInheritanceRelationship.SubClass == null)
            {
                DMClass dmClass = CreateSavedDMClassWithProps();
                dmInheritanceRelationship.SubClass = dmClass;
            }
            if (dmInheritanceRelationship.InheritanceType == ORMapping.ClassTableInheritance
                && dmInheritanceRelationship.ID == null)
            {
                dmInheritanceRelationship.Identity = dmInheritanceRelationship.SuperClass.Properties[0].PropertyName;
            }
            if (dmInheritanceRelationship.InheritanceType == ORMapping.SingleTableInheritance
                && dmInheritanceRelationship.Discriminator == null)
            {
                dmInheritanceRelationship.Discriminator = dmInheritanceRelationship.SuperClass.Properties[0].PropertyName;
            }
            return dmInheritanceRelationship;
        }
/*
        public static DMPropertyType CreateSavedPropertyTypeInfo(DMSolution dmSolution)
        {
            DMPropertyType dmPropertyType = CreateUnsavedValidPropertyTypeInfo();
            //            dmPropertyType.Solution = dmSolution;
            dmPropertyType.Save();
            return dmPropertyType;
        }*/

        public static DMPropertyType CreateSavedPropertyTypeInfo()
        {
            DMPropertyType dmPropertyType = CreateUnsavedValidPropertyTypeInfo();
            dmPropertyType.Save();
            return dmPropertyType;
        }

        public static DMPropertyType CreateUnsavedValidPropertyTypeInfo()
        {
            DMPropertyType dmPropertyType = new DMPropertyType();
            return UpdatePropertyTypeInfoWithValidData(dmPropertyType);
        }

        private static DMPropertyType UpdatePropertyTypeInfoWithValidData(DMPropertyType dmPropertyType)
        {
            if (dmPropertyType.AssemblyName == null) dmPropertyType.AssemblyName = GetRandomString();
            if (dmPropertyType.PropertyTypeName == null) dmPropertyType.PropertyTypeName = GetRandomString();
            return dmPropertyType;
        }

        private static UIControlType CreateSavedUIControlTypeInfo(DMSolution dmSolution)
        {
            UIControlType controlType = CreateUnsavedValidUIControlTypeInfo();
            controlType.Solution = dmSolution;
            controlType.Save();
            return controlType;
        }
/*
        public static UIControlType CreateSavedUIControlTypeInfo()
        {
            UIControlType controlType = CreateUnsavedValidUIControlTypeInfo();
            controlType.Save();
            return controlType;
        }*/

        public static UIControlType CreateUnsavedValidUIControlTypeInfo()
        {
            UIControlType controlType = new UIControlType();
            return UpdateUIControlTypeInfoWithValidData(controlType);
        }

        private static UIControlType UpdateUIControlTypeInfoWithValidData(UIControlType controlType)
        {
            if (controlType.AssemblyName == null) controlType.AssemblyName = GetRandomString();
            if (controlType.TypeName == null) controlType.TypeName = GetRandomString();
            if (controlType.Solution == null) controlType.Solution = CreateSavedDMSolution();
            return controlType;
        }
/*
        public static UIControlMapperType CreateSavedUIControlMapperTypeInfo(DMSolution dmSolution)
        {
            UIControlMapperType controlMapperType = CreateUnsavedValidUIControlMapperTypeInfo();
            controlMapperType.Solution = dmSolution;
            controlMapperType.Save();
            return controlMapperType;
        }

        public static UIControlMapperType CreateSavedUIControlMapperTypeInfo()
        {
            UIControlMapperType controlMapperType = CreateUnsavedValidUIControlMapperTypeInfo();
            controlMapperType.Save();
            return controlMapperType;
        }*/

        public static UIControlMapperType CreateUnsavedValidUIControlMapperTypeInfo()
        {
            UIControlMapperType controlMapperType = new UIControlMapperType();
            return UpdateUIControlMapperTypeInfoWithValidData(controlMapperType);
        }

        private static UIControlMapperType UpdateUIControlMapperTypeInfoWithValidData
            (UIControlMapperType controlMapperType)
        {
            if (controlMapperType.AssemblyName == null) controlMapperType.AssemblyName = GetRandomString();
            if (controlMapperType.TypeName == null) controlMapperType.TypeName = GetRandomString();
            return controlMapperType;
        }

        private static UIGridColumnControlType CreateSavedUIGridColumnControlTypeInfo(IDMSolution dmSolution)
        {
            UIGridColumnControlType gridColumnControlType = CreateUnsavedValidUIGridColumnControlTypeInfo((DMSolution) dmSolution);
            gridColumnControlType.Save();
            return gridColumnControlType;
        }

/*
        public static UIGridColumnControlType CreateSavedUIGridColumnControlTypeInfo()
        {
            DMSolution dmSolution = CreateSavedDMSolution();
            return CreateSavedUIGridColumnControlTypeInfo(dmSolution);
        }
*/

        public static UIGridColumnControlType CreateUnsavedValidUIGridColumnControlTypeInfo()
        {
            DMSolution dmSolution = CreateSavedDMSolution();
            return CreateUnsavedValidUIGridColumnControlTypeInfo(dmSolution);
        }

        private static UIGridColumnControlType CreateUnsavedValidUIGridColumnControlTypeInfo(DMSolution dmSolution)
        {
            UIGridColumnControlType gridColumnControlType = dmSolution.UIGridColumnControlTypes.CreateBusinessObject();
            return UpdateUIGridColumnControlTypeInfoWithValidData(gridColumnControlType);
        }

        private static UIGridColumnControlType UpdateUIGridColumnControlTypeInfoWithValidData
            (UIGridColumnControlType gridColumnControlType)
        {
            if (gridColumnControlType.AssemblyName == null) gridColumnControlType.AssemblyName = GetRandomString();
            if (gridColumnControlType.TypeName == null) gridColumnControlType.TypeName = GetRandomString();
            return gridColumnControlType;
        }
/*

        public static DMClass CreateSavedDMClassWithUIInfo()
        {
            DMClass dmClass = CreateSavedDMClass();
            UIView uiView = CreateSavedUIInfo(dmClass);
            UIGridInfo grid = CreateSavedUIGridInfo(uiView);
            UIGridColumnInfo gridColumn = CreateSavedUIColumnInfo(grid);
            CreateSavedUIParameterInfo(gridColumn);

            uiView.UIGridInfo = grid;
            uiView.Save();
            return dmClass;
        }

        public static DMClass CreateSavedDMClassWithUIInfo_GridAndForm()
        {
            DMClass dmClass = CreateSavedDMClass();
            UIView uiView = CreateSavedUIInfo(dmClass);
            UIGridInfo grid = CreateSavedUIGridInfo(uiView);
            UIGridColumnInfo gridColumn = CreateSavedUIColumnInfo(grid);
            CreateSavedUIParameterInfo(gridColumn);
            UIFormInfo uiFormInfo = CreateSavedUIFormInfo(uiView);
            uiView.UIGridInfo = grid;
            uiView.UIFormInfo = uiFormInfo;
            uiView.Save();
            return dmClass;
        }

        public static UIGridFilter CreateSavedUIGridFilterInfo()
        {
            UIGridInfo grid = CreateSavedUIGridInfo();
            UIGridFilter gridFilter = new UIGridFilter {UIGridInfo = grid};
            gridFilter.Save();
            UIGridFilterProp gridFilterProp = gridFilter.UIGridFilterProps.CreateBusinessObject();
            gridFilterProp.PropertyName = TestUtilsDMProperty.CreateSavedDMProperty().PropertyName;
            gridFilterProp.LabelText = GetRandomString();
            //TODO Brett 30 Mar 2009:             gridFilterProp.FilterControlType = FilterControlTypeInfo.StringFilterTextBox;
            gridFilterProp.FilterClauseOp = FilterClauseOperator.OpLike;
            gridFilter.Save();
            return gridFilter;
        }
*/

        public static UIGridFilter CreateUnsavedUIGridFilterInfo()
        {
            UIGridInfo grid = CreateSavedUIGridInfo();
            UIGridFilter gridFilter = new UIGridFilter {UIGridInfo = grid};
            CreateUnsavedGridFilterProp(gridFilter);
            return gridFilter;
        }

// ReSharper disable UnusedMethodReturnValue.Local
        private static DMProperty CreateUnsavedGridFilterProp(UIGridFilter gridFilter)
// ReSharper restore UnusedMethodReturnValue.Local
        {
            UIGridFilterProp gridFilterProp = gridFilter.UIGridFilterProps.CreateBusinessObject();
            DMProperty property = TestUtilsDMProperty.CreateSavedDMProperty();
            gridFilterProp.PropertyName = property.PropertyName;
            gridFilterProp.LabelText = GetRandomString();
            //TODO Brett 30 Mar 2009:             gridFilterProp.FilterControlType = FilterControlTypeInfo.StringFilterTextBox;
            gridFilterProp.FilterClauseOp = FilterClauseOperator.OpLike;
            return property;
        }
/*
        public static UIColumnLayout CreateSavedUIColumnLayoutWithTwoFieldInfos()
        {
            UIColumnLayout columnLayout = CreateSavedUIColumnLayoutInfo();
            CreateSavedUIField(columnLayout);
            CreateSavedUIField(columnLayout);
            return columnLayout;
        }


        public static UITab CreateSavedUITabInfoWithTwoColumnLayoutInfos()
        {
            UITab tab = CreateSavedUITabInfo();
            CreateSavedUIColumnLayoutInfo(tab);
            CreateSavedUIColumnLayoutInfo(tab);
            return tab;
        }

        public static UIFormInfo CreateSavedUIFormInfoWithTwoTabInfos()
        {
            UIFormInfo uiFormInfo = CreateSavedUIFormInfo();
            CreateSavedUITabInfo(uiFormInfo);
            CreateSavedUITabInfo(uiFormInfo);
            return uiFormInfo;
        }*/

        public static DMUniqueConstraint CreateSavedUniqueConstraintInfoWithUniqueConstraintProperties()
        {
            DMUniqueConstraint dmUniqueConstraint = CreateSavedUniqueConstraintInfo();
            CreateSavedRelatedPropertyInfo(dmUniqueConstraint);
            CreateSavedRelatedPropertyInfo(dmUniqueConstraint);
            return dmUniqueConstraint;
        }

        #endregion

        public static void AssertStringEmpty(string stringValue, string stringName)
        {
            AssertStringEmpty(stringValue, stringName, stringName + " Should not be empty");
        }

        public static void AssertStringNotEmpty(string stringValue, string stringName)
        {
            Assert.IsFalse(String.IsNullOrEmpty(stringValue), stringName + " Should not be null");
        }

        public static void AssertStringEmpty(string stringValue, string stringName, string errorMessage)
        {
            Assert.IsTrue(String.IsNullOrEmpty(stringValue), stringName + " : " + errorMessage);
        }

        public static T AssertIsInstanceOf<T>(object obj)
        {
            Assert.IsInstanceOf(typeof(T), obj);
            return (T)obj;
        }

  /*      public static T AssertIsInstanceOf<T>(object obj, string message)
        {
            Assert.IsInstanceOf(typeof(T), obj, message);
            return (T)obj;
        }
*/
        public static TRuleType AssertHasRuleOfType<TRuleType>(IEnumerable<IBusinessObjectRule> rules)
                where TRuleType : class, IBusinessObjectRule 
        {
            List<IBusinessObjectRule> rulesList = new List<IBusinessObjectRule>(rules);
            TRuleType businessObjectRule = rulesList.Find(rule => rule is TRuleType) as TRuleType;
            Assert.IsTrue(businessObjectRule != null);
            return businessObjectRule;
        }
        #region Random Stuff

        private static Random _randomGenerator;

        private static Random Random
        {
            get { return _randomGenerator ?? (_randomGenerator = new Random()); }
        }

        public static int GetRandomInt()
        {
            return GetRandomInt(100000);
        }

        public static int GetRandomInt(int max)
        {
            return Random.Next(0, max);
        }

        public static int GetRandomInt(int min, int max)
        {
            return Random.Next(min, max);
        }

        public static string GetRandomString()
        {
            return "A" + Guid.NewGuid().ToString().Replace("-", "");
        }

        public static string GetRandomString(int maxLength)
        {
            if (maxLength > 9) maxLength = 9;
            int max = Convert.ToInt32(Math.Pow(10, maxLength)) - 1;
            return "A" + GetRandomInt(max).ToString();
        }

        public static string GetRandomString(int minLength, int maxLength)
        {
            if (minLength > 9) minLength = 9;
            int min = Convert.ToInt32(Math.Pow(9, minLength)) - 2;
            min++;
            if (maxLength > 9) maxLength = 9;
            int max = Convert.ToInt32(Math.Pow(9, maxLength)) - 1;
            return "A" + GetRandomInt(min, max);
        }

        public static bool GetRandomBoolean()
        {
            return (GetRandomInt(100000) > 50000);
        }

        public static DateTime GetRandomDate()
        {
            return DateTime.Now;
        }

/*        public static DateTime GetRandomDate(string max)
        {
            string start = DateTime.MinValue.ToString("yyyy/MM/dd");
            return GetRandomDate(start, max);
        }*/
/*
        public static DateTime GetRandomDate(string min, string max)
        {
            DateTime start = DateTime.Parse(min);

            int range = (DateTime.Parse(max) - start).Days;
            return start.AddDays(GetRandomInt(range));
        }*/

 /*       /// <summary>
        /// Takes a lookup list generated by Habanero and randomly selects a value
        /// from the list
        /// </summary>
        public static object GetRandomLookupListValue(Dictionary<string, object> lookupList)
        {
            object[] values = new object[lookupList.Count];
            lookupList.Values.CopyTo(values, 0);
            return values[GetRandomInt(0, values.Length - 1)];
        }
*/
        public static TEnum GetRandomEnum<TEnum>()
            where TEnum : struct
        {
            return GetRandomEnum<TEnum>(null);
        }

        public static TEnum GetRandomEnum<TEnum>(TEnum? excluded)
            where TEnum : struct
        {
            Array values = Enum.GetValues(typeof(TEnum));
            int randomIndex = GetRandomInt(0, values.Length);
            TEnum value = (TEnum)values.GetValue(randomIndex);
            if (excluded.HasValue && excluded.Value.Equals(value))
            {
                return GetRandomEnum(excluded);
            }
            return value;
        }

        #endregion

        public static void AssertItemsAreInOrder(params IOrderableObject[] orderableObjects)
        {
            int ordinalPosition = 0;
            for (int i = 0; i < orderableObjects.Length; i++)
            {
                IOrderableObject orderableObject = orderableObjects[i];
                Assert.IsTrue(orderableObject.OrdinalPosition.HasValue, "Ordinal position not set for object " + (i + 1));
                int itemOrdinalPosition = orderableObject.OrdinalPosition.GetValueOrDefault(0);
                Assert.That(itemOrdinalPosition, Is.GreaterThan(ordinalPosition), "Ordinal position incorrect for object " + (i + 1));
                ordinalPosition = itemOrdinalPosition;
            }
        }
    }
}